<template>
  <el-card class="box-card" style="width: 1180px">   
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-button type="success" @click="dialog.addshow = true"
            >添加</el-button
          >     
        </el-form-item>
      </el-form>
    </div>      
    <el-table :data="bankList" border style="width: 100%">
        <el-table-column prop="bankId" label="编号" width="180">
</el-table-column>
      <el-table-column label="图片" width="130">
                <template slot-scope="scope">                   
                               <img
              :src="scope.row.bankLogo"
              alt=""
              style="width: 60px; height: 60px"
            />
                  
                </template>
            </el-table-column>
      <el-table-column label="银行状态" width="180">启用
      </el-table-column>
      <el-table-column prop="bankName" label="银行名称" width="180">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="edit(scope.row.bankId)"
            >编辑</el-button
          >
          <el-popconfirm title="这是一段内容确定删除吗？" @confirm="del(scope.row.bankId)">
            <el-button
            slot="reference"
            size="mini"
            type="danger"           
            >取消合作</el-button>
            
          </el-popconfirm>
        </template>
      </el-table-column>      
    </el-table>
    <el-dialog title="添加银行" :visible.sync="dialog.addshow" width="30%">
      <el-form ref="addForm" :model="addForm" status-icon :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="bankName">
              <el-input v-model="addForm.bankName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="isStartUse">
              <el-input v-model="addForm.isStartUse" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="银行图标">
              <el-upload
                class="avatar-uploader"
                action="http://localhost:8888/upload"
                name="file"
                :headers="upload.headerObj"
                :show-file-list="false"
                :on-success="handleSuccess"
                :before-upload="beforeUpload"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.addshow = false">取 消</el-button>
        <el-button type="primary" @click="add('addForm')">提 交</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改银行" :visible.sync="dialog.updateshow" width="30%">
      <el-form ref="updateForm" :model="updateForm" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="编号" prop="bankId">
              <el-input v-model="updateForm.bankId"  ></el-input>
            </el-form-item>
            <el-form-item label="名称" prop="bankName">
              <el-input v-model="updateForm.bankName"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="isStartUse">
              <el-input v-model="updateForm.isStartUse"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.updateshow = false">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data() {
     var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入银行名称'));
        } else {
          if (this.addForm.bankName !== '') {
            this.$refs.addForm.validateField('bankName');
          }
          callback();
        }
      };
    return {
      rules: {
          bankName: [
            { validator: validatePass, trigger: 'blur' }
          ],},
        imageUrl: "",
      upload: {
        headerObj: {
          //Spring security时需要传递token，否则不需要该配置
          Authorization: sessionStorage.getItem("authorization"),
        },
      },
        bankList:[],
      query:{
      },
      dialog:{
        addshow:false,
        updateshow:false
      },
      addForm:{
        bankName:""
      },
      updateForm:{},
    };
  },
  methods: {
    handleSuccess(res, file) {
      if (res.code == 200) {
        this.imageUrl = res.message;
        this.addForm.bankLogo = res.message; //将返回的文件储存路径赋值picture字段
      }
    },
    beforeUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    add(formName){
      this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
        this.$axios.post("/banks",this.addForm)
        .then(res=>{
            if(res.data.code==200){
                this.dialog.addshow=false;
                this.loadBanks();
            }
        })
    },
    edit(id){
        this.$axios.get("/banks/"+id)
        .then(res=>{
            if(res.data.code==200){
                this.updateForm=res.data.data.bank;
            }
        })
        this.dialog.updateshow=true;
    },
    del(id){
        this.$axios.delete("/banks/"+id)
        .then(res=>{
            if(res.data.code==200){
                this.$message.success("删除成功")
                this.loadBanks();
            }
        })
    },
    update(){
        this.$axios.put("/banks",this.updateForm)
        .then(res=>{
            if(res.data.code==200){
                this.dialog.updateshow=false;
                this.loadBanks();
            }
        })
    },
    loadBanks(){
        this.$axios.get("/banks")
        .then(res=>{
            this.bankList=res.data.data.list;
        })
    },
  },
  mounted(){
    this.loadBanks();
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>